<!DOCTYPE html>
<html>
<include file="layer:layer_head"/>
<body>
    <!-- 内容主体区域 -->
    <div style="padding:15px;height: 100%;">
        <div class="layui-row">
            <div class="layui-col-xs12">
                <div class="layui-form">
                    <input type="text" class="layui-hide" name="admin_id" value="<php>echo $_GET['admin_id'];</php>">
                    <div class="layui-form-item">
                        <label class="layui-form-label"> <span style="color: #FF5722;">*</span> 用户账号 </label>
                        <div class="layui-input-block">
                            <input type="text" style="width: 50%;" class="layui-input" name="username" placeholder="用户账号" required lay-verify="required" value="<{$user.username}>">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"> 密码 </label>
                        <div class="layui-input-block">
                            <input type="text" style="width: 50%;" class="layui-input" name="password" placeholder="密码">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"> 权限 </label>
                        <div class="layui-input-block layui-col-space15" id="auth">
                            <foreach name="auth" item="m">
                            <div class="layui-col-xs4">
                                <label class="layui-col-xs12" style="color: #009688;text-align: left;"> <{$m.name}> </label>
                                <div class="checkbox layui-col-xs12" style="display: <php>
                                        if(count($m['menu']) == 0)
                                        echo 'block;';
                                        else
                                        echo 'none;';
                                    </php>;">
                                    <label class="layui-col-xs8 layui-elip layui-form-switch-label"> <{$m.name}> </label>
                                    <input type="checkbox" name="<{$m.action}>" lay-skin="switch" lay-text="ON|OFF"
                                    <php>
                                        if($m['show'] === true)
                                        echo "checked";
                                    </php>
                                    >
                                </div>
                                <foreach name="m['menu']" item="me">
                                <div class="checkbox layui-col-xs12">
                                    <label class="layui-col-xs8 layui-elip layui-form-switch-label"> <{$me.name}> </label>
                                    <input type="checkbox" name="<{$me.action}>" lay-skin="switch" lay-text="ON|OFF"
                                    <php>
                                        if($me['show'] === true)
                                        echo "checked";
                                    </php>
                                    >
                                </div>
                                </foreach>
                            </div>
                            </foreach>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="addAdmin"
                            <php>
                                if(isset($_GET['admin_id']))
                                echo "style='display:none;'";
                            </php>
                            >
                                <i class="iconfont icon-jiajianzujianjiahao"></i>
                                添加
                            </button>
                            <button class="layui-btn layui-btn-danger" lay-submit lay-filter="updateAdmin"
                            <php>
                                if(!isset($_GET['admin_id']))
                                echo "style='display:none;'";
                            </php>
                            >
                            <i class="iconfont icon-pingjia"></i>
                            更新
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<include file="layer:layer_js"/>
<!--自己的js代码引入-->
<script type="text/javascript">
layui.use('form',function () {
    var form = layui.form;
    form.on('submit(addAdmin)',function (data) {
        var json = data.field;
        var result = getAuth(json);
        json = result.json;
        json.auth = result.auth.join(",",result.auth);
        if(json.auth == ''){
            return layer.msg("请至少选择一项权限!",{icon:5,time:1000});
        }
        var add_url = root+"/admin/admin/addAdmin";
        $.post(add_url,json,function (res) {
            if(res.status){
                dialog.success(res.info,function () {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                });
            }else{
                dialog.error(res.info);
            }
        });
        return false;
    });

    form.on("submit(updateAdmin)",function (data) {
        var json = data.field;
        var result = getAuth(json);
        json = result.json;
        json.auth = result.auth.join(",",result.auth);
        if(json.auth == ''){
            return layer.msg("请至少选择一项权限!",{icon:5,time:1000});
        }
        var url = root+"/admin/admin/updateAdmin";
        $.post(url,json,function (res) {
            if(res.status){
                return dialog.success(res.info,function () {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.location.reload();
                    parent.layer.close(index); //再执行关闭
                });
            }else{
                return dialog.error(res.info);
            }
        });
        return false;
    });
});

    function getAuth(json) {
        var param = [];
        $.each(json,function (k,v) {
            if(k != 'username' && k != 'password' && k != 'admin_id'){
                param.push(k);
                delete json[k];
            }
            if(k == 'admin_id'){
                if(v == ''){
                    delete json[k];
                }
            }
        });
        return {"auth":param,"json":json};
    }
</script>
</html>